"""
Case Type   : SQL_LIMIT
Case Name   : 创建规则时，limit_type字段输入值为多个关键字，合理报错
Create At   : 2025/9/16
Owner       : lonely-dance
Description :
    1、修改参数enable_sql_limit=on;
    2、创建限流limit，limit_type字段输入值为多个关键字
    3、清理环境
Expect      :
    1、修改成功
    2、创建失败，合理报错
    3、清理环境成功
History     :
"""

import os
import unittest
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class SqlLimit(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----- {os.path.basename(__file__)} start-----')
        self.comm_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.Constant = Constant()
        self.limit_name1 = 'limit0018_01'

    def test_main(self):
        text = '----step1:修改参数enable_sql_limit=on expect:成功----'
        self.log.info(text)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = on')

        text = '----step2:创建限流limit，limit_type字段输入值为多个关键字 expect：创建失败，合理报错----'
        self.log.info(text)
        cmd = f'''
        select gs_create_sql_limit('{self.limit_name1}','UPDATE, SELECT',0,100,'now()','2035-3-30 23:59:59','{{{{UPDATE,set}}}}','','');
        '''
        self.log.info(cmd)
        res = self.comm_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertEqual(
            'ERROR:  limit_type UPDATE, SELECT is invalid. expected: sqlid, select, update, insert, delete',
            str(res.splitlines()[0].strip()), '执行失败' + text)

    def tearDown(self):
        text = '----step3:恢复环境 expect:成功----'
        self.log.info(text)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = off')
        self.log.info(f'----- {os.path.basename(__file__)} end-----')
